<template>
  <el-tooltip class="item" effect="dark" content="表格筛选" placement="top-start" style="margin-right: 10px">
    <el-dropdown :hide-on-click="false">
      <el-button size="small" icon="el-icon-s-tools"></el-button>
      <el-dropdown-menu slot="dropdown">
        <el-checkbox-group v-model="checkList" @change="checkChange">
          <el-dropdown-item v-for="item in dropList" :key="item.prop">
            <el-checkbox :disabled="checkDisabled(item.prop)" :label="item.prop"
              :value="item.prop">{{ item.label }}</el-checkbox>
          </el-dropdown-item>
        </el-checkbox-group>
      </el-dropdown-menu>
    </el-dropdown>
  </el-tooltip>
</template>

<script>
export default {
  props: {
    dropList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      checkList: [],
    }
  },
  created() {
    this.initCheck()
  },
  methods: {
    // 只剩最后一项时禁用
    checkDisabled(val) {
      if (this.checkList.length === 1 && this.checkList.includes(val)) {
        return true
      }
      return false
    },
    // 初始化checklist
    initCheck() {
      let drop = this.dropList.map(item => item.prop)
      this.checkList = [...drop]
    },
    checkChange(val) {
      this.$emit('check-change', val)
    }
  },
}
</script>

<style lang="scss" scoped>
.el-checkbox-group {
  display: flex;
  flex-direction: column;
}

::v-deep .el-dropdown-menu__item,
.el-dropdown-menu:hover {
  background: #fff;
}
</style>